package com.rede.didiok.user.modulelist.rank.web;


import java.util.Collection;
import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.rede.didiok.common.annotion.AvoidRepeatableCommit.AvoidRepeatableCommit;
import com.rede.didiok.common.dto.rank.MemberCommentDto;
import com.rede.didiok.common.exception.ThrowableUtils;
import com.rede.didiok.common.utils.R;
import com.rede.didiok.common.utils.ResultUtil;
import com.rede.didiok.common.validator.group.Delete;
import com.rede.didiok.common.validator.group.GetList;
import com.rede.didiok.common.validator.group.Insert;
import com.rede.didiok.common.validator.group.Update;
import com.rede.didiok.user.modulelist.rank.service.MemberCommentService;

/**
 * 评论表 RestApi
 *
 * @author 陌溪
 * @since 2020年1月20日16:44:25
 */
@Api(value = "用户评论相关接口", tags = {"用户评论相关接口"})
@RestController
@RequestMapping("/rank/feign/memberComment")
@Slf4j
public class MemberCommentController {

    @Autowired
    MemberCommentService memberCommentService;

    @ApiOperation(value = "获取评论列表", notes = "获取评论列表", response = String.class)
    @PostMapping(value = "/getList")
    public String getList(@Validated({GetList.class}) @RequestBody MemberCommentDto memberCommentDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("获取评论列表: {}", memberCommentDto);
        return ResultUtil.successWithData(memberCommentService.getPageList(memberCommentDto));
    }

    @AvoidRepeatableCommit
    @ApiOperation(value = "增加评论", notes = "增加评论", response = String.class)
    @PostMapping("/add")
    public String add(@Validated({Insert.class}) @RequestBody MemberCommentDto memberCommentDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("新增评论: {}", memberCommentDto);
        return ResultUtil.successWithData(memberCommentService.addComment(memberCommentDto));
    }

    @ApiOperation(value = "编辑评论", notes = "编辑评论", response = String.class)
    @PostMapping("/edit")
    public String edit(@Validated({Update.class}) @RequestBody MemberCommentDto memberCommentDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("编辑评论: {}", memberCommentDto);
        return ResultUtil.successWithMessage(memberCommentService.editComment(memberCommentDto));
    }

    @ApiOperation(value = "删除评论", notes = "删除评论", response = String.class)
    @PostMapping("/delete")
    public String delete(@Validated({Delete.class}) @RequestBody MemberCommentDto memberCommentDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("删除评论: {}", memberCommentDto);
        return ResultUtil.successWithMessage(memberCommentService.deleteComment(memberCommentDto));
    }

    @ApiOperation(value = "删除选中评论", notes = "删除选中评论", response = String.class)
    @PostMapping("/deleteBatch")
    public String deleteBatch(@Validated({Delete.class}) @RequestBody List<MemberCommentDto> commentDtoList, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return ResultUtil.successWithMessage(memberCommentService.deleteBatchComment(commentDtoList));
    }

    @ApiOperation(value = "审核评论", notes = "审核评论", response = String.class)
    @PostMapping("/audit")
    public String audit(@RequestBody MemberCommentDto memberCommentDto) {
        return ResultUtil.successWithMessage(memberCommentService.auditComment(memberCommentDto));
    }

    /**
     * 评论下架
     * @param uid
     * @return
     */
    @PostMapping(value = "/revocation")
    R revocationComment(@RequestParam("uid") String uid){
        return R.ok().setData(memberCommentService.revocationComment(uid));

    }
    /**
     * 根据id获取榜单成员评论
     * @param
     * @return
     */
    @GetMapping("/getCommentById")
    R getCommentById(@RequestParam("uid") String uid){
        return R.ok().setData(memberCommentService.getCommentById(uid));
    }

    /**
     * 根据用户列表ID  查询各用户评论数
     * @param userUids
     * @return
     */
    @GetMapping("/getCommentCountByUserIds")
    R getCommentCountByUserIds(@RequestParam("userUids") List<String> userUids){
        return R.ok().setData(memberCommentService.getCommentCountByUserIds(userUids));
    }
    /**
     * 获取评论数量
     * @param createUserUid
     * @param memberUid
     * @param toUserUid
     * @return
     */
    @GetMapping("/getCommentCount")
    Integer getCommentCount(@RequestParam("createUserUid")String createUserUid, @RequestParam("memberUid")String memberUid, @RequestParam("toUserUid")String toUserUid){
        Integer commentCount = memberCommentService.getCommentCount(createUserUid, memberUid, toUserUid);
        return commentCount;
    }
    /**
     * 根据id集合查询评论
     * @param uidList
     * @return
     */
    @GetMapping("/getCommentPageListByIds")
    String getCommentPageListByIds(@RequestParam("uidList") Collection<String> uidList){
        return ResultUtil.successWithData(memberCommentService.getCommentPageListByIds(uidList));
    }

    /**
     * 添加评论（当该评论不是一级评论时，需要设置一级评论UID字段）
     * @param comment
     * @return
     */
    @PostMapping("/addCommentWithFirstCommentUid")
    R addCommentWithFirstCommentUid(@RequestBody MemberCommentDto comment){
        return R.ok().setData(memberCommentService.addCommentWithFirstCommentUid(comment));
    }

    /**
     * 移除该文章下所有评论
     * @param memberUidList
     * @return
     */
    @RequestMapping(value = "/batchDeleteCommentByMemberUid", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = "application/json;charset=UTF-8")
    R batchDeleteCommentByMemberUid(@RequestBody List<String> memberUidList){
        memberCommentService.batchDeleteCommentByMemberUid(memberUidList);
        return R.ok();
    }
}

